﻿Imports System.Xml.Serialization
Imports System.IO
Public Class MainForm
    Public WithEvents AllCharacters As New Characters
    Private IsSaved As Boolean = False
    Private SaveFile As String = Application.LocalUserAppDataPath & "\herotracker.xml"
    Private SelectedCharacter As New Character


    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If AllCharacters.Count > 0 Then
            Save()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If File.Exists(SaveFile) Then
            Dim fs As New FileStream(SaveFile, FileMode.Open)
            Dim loader As New XmlSerializer(GetType(Characters))
            AllCharacters = loader.Deserialize(fs)
            fs.Close()
            If AllCharacters.Count > 0 Then
                ShowByPhase(Me.CurrentPhaseLabel.Text)
            End If
        End If
    End Sub

    Private Sub Save()
        Dim loader As New XmlSerializer(GetType(Characters))
        Dim fs As New FileStream(SaveFile, FileMode.Create)
        loader.Serialize(fs, AllCharacters)
        fs.Close()
        IsSaved = True
    End Sub

    Private Sub RightArrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightArrow.Click
        If Me.CharacterTreeView.Nodes.Count > 0 Then
            If MsgBox("There are characters that have not acted yet this phase. Advance to next phase anyway?", MsgBoxStyle.YesNo, "Hero Tracker") = MsgBoxResult.Yes Then
                NextPhase(Me.CurrentPhaseLabel.Text)
            End If
        Else
            NextPhase(Me.CurrentPhaseLabel.Text)
        End If
    End Sub

    Private Sub NextPhase(ByVal currentphase As String)
        If currentphase = "12" Then
            Me.CurrentPhaseLabel.Text = "12+"
        ElseIf currentphase = "12+" Then
            Me.CurrentPhaseLabel.Text = "1"
        Else
            Me.CurrentPhaseLabel.Text = (CInt(currentphase) + 1).ToString
        End If

        For Each c As Character In AllCharacters
            c.IsActed = False
        Next
        ShowByPhase(Me.CurrentPhaseLabel.Text)
    End Sub

    Private Sub ShowByPhase(ByVal phase As Integer)
        Me.CharacterTreeView.Nodes.Clear()
        For Each c As Character In AllCharacters
            If Not c.IsActed Then
                For Each i As Integer In c.Phases
                    If phase = i Then
                        Dim n As New TreeNode
                        If c.NPC Then
                            n.Text = c.Name
                            n.Name = c.Name
                            Me.CharacterTreeView.Nodes.Add(n)
                            n.NodeFont = New Font("Arial", 10, FontStyle.Italic, GraphicsUnit.Point)
                            n.ContextMenuStrip = TreeViewContextMenu
                            n.EnsureVisible()
                        Else
                            n.Text = c.Name
                            n.Name = c.Name
                            Me.CharacterTreeView.Nodes.Add(n)
                            n.NodeFont = New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
                            n.ContextMenuStrip = TreeViewContextMenu
                            n.EnsureVisible()
                        End If
                        Exit For
                    End If
                Next
            End If
        Next
    End Sub

    Private Sub ShowByPhase(ByVal phase As String)
        If phase = "12+" Then
            Me.CharacterTreeView.Nodes.Clear()
            For Each c As Character In AllCharacters
                If Not c.IsActed Then
                    Dim n As New TreeNode
                    If c.NPC Then
                        n.Text = c.Name
                        n.Name = c.Name
                        Me.CharacterTreeView.Nodes.Add(n)
                        n.NodeFont = New Font("Arial", 10, FontStyle.Italic, GraphicsUnit.Point)
                        n.ContextMenuStrip = TreeViewContextMenu
                        n.EnsureVisible()
                    Else
                        n.Text = c.Name
                        n.Name = c.Name
                        Me.CharacterTreeView.Nodes.Add(n)
                        n.NodeFont = New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
                        n.ContextMenuStrip = TreeViewContextMenu
                        n.EnsureVisible()
                    End If
                End If
            Next
        Else
            ShowByPhase(CInt(phase))
        End If
    End Sub

    Private Sub CharacterTreeView_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles CharacterTreeView.AfterCheck
        If e.Node.Checked Then
            Me.CharacterTreeView.Nodes.Remove(e.Node)
        End If
    End Sub

    Private Sub LeftArrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftArrow.Click
        Dim pd As New PreviousPhase
        pd.Phase = Me.CurrentPhaseLabel.Text
        pd.ShowDialog()
        If pd.DialogResult = Windows.Forms.DialogResult.Yes Then
            For Each c As Character In AllCharacters
                c.IsActed = False
            Next
            If Me.CurrentPhaseLabel.Text = "12+" Then
                Me.CurrentPhaseLabel.Text = "12"
            ElseIf Me.CurrentPhaseLabel.Text = "12" Then
                Me.CurrentPhaseLabel.Text = "1"
            Else
                Me.CurrentPhaseLabel.Text = (CInt(Me.CurrentPhaseLabel.Text) - 1).ToString
            End If
            ShowByPhase(Me.CurrentPhaseLabel.Text)
        End If
    End Sub

    Private Sub CharacterTreeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles CharacterTreeView.AfterSelect
        SetBindings(AllCharacters.Find(e.Node.Text))
    End Sub

    Private Sub OnCharacterAdded(ByVal item As Character) Handles AllCharacters.CharacterAdded
        ShowByPhase(Me.CurrentPhaseLabel.Text)
    End Sub

    Private Sub SetBindings(ByVal boundcharacter As Character)
        For Each ctrl As Control In Me.Controls
            If ctrl.DataBindings.Count > 0 Then
                ctrl.DataBindings.Clear()
            End If
        Next

        Me.BodyLabel.DataBindings.Add("Text", boundcharacter, "CurrentBody")
        Me.DCVLabel.DataBindings.Add("Text", boundcharacter, "DCV")
        Me.DexterityLabel.DataBindings.Add("Text", boundcharacter, "Dexterity")
        Me.EDLabel.DataBindings.Add("Text", boundcharacter, "ED")
        Me.EgoLabel.DataBindings.Add("Text", boundcharacter, "Ego")
        Me.EnduranceLabel.DataBindings.Add("Text", boundcharacter, "CurrentEndurance")
        Me.mDCVLabel.DataBindings.Add("Text", boundcharacter, "mDCV")
        Me.MDLabel.DataBindings.Add("Text", boundcharacter, "MD")
        Me.mOCVLabel.DataBindings.Add("Text", boundcharacter, "mOCV")
        Me.OCVLabel.DataBindings.Add("Text", boundcharacter, "OCV")
        Me.PDLabel.DataBindings.Add("Text", boundcharacter, "PD")
        Me.PowerDefenseLabel.DataBindings.Add("Text", boundcharacter, "PowerDefense")
        Me.RecoveryLabel.DataBindings.Add("Text", boundcharacter, "Recovery")
        Me.rEDLabel.DataBindings.Add("Text", boundcharacter, "rED")
        Me.rPDLabel.DataBindings.Add("Text", boundcharacter, "rPD")
        Me.SpeedLabel.DataBindings.Add("Text", boundcharacter, "Speed")
        Me.StunLabel.DataBindings.Add("Text", boundcharacter, "CurrentStun")
    End Sub

    Private Sub NewCharacterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewCharacterToolStripMenuItem.Click
        Dim nc As New EditCharacter(True)
        nc.ShowDialog()
        If nc.DialogResult = Windows.Forms.DialogResult.Yes Then
            'new character added, repopulate current phase
            ShowByPhase(Me.CurrentPhaseLabel.Text)
        End If
    End Sub

    Private Sub EditToolStripMenuItem_DropDownOpening(ByVal sender As Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem.DropDownOpening
        For Each c As Character In AllCharacters
            Dim ec As New ToolStripMenuItem(c.Name, Nothing, AddressOf Me.EditCharacterSubMenuItem)
            Me.EditToolStripMenuItem.DropDownItems.Add(ec)
        Next
    End Sub

    Private Sub EditCharacterSubMenuItem(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim menu As ToolStripMenuItem = sender
        Dim ec As New EditCharacter(False, AllCharacters.Find(menu.Text))
        ec.ShowDialog()
        If ec.DialogResult = Windows.Forms.DialogResult.Yes Then
            ShowByPhase(Me.CurrentPhaseLabel.Text)
            Me.CharacterTreeView.SelectedNode = Me.CharacterTreeView.Nodes.Find(menu.Text, True)(0)
            Save()
        End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Save()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub RecoverToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RecoverToolStripMenuItem.Click
        Dim n As TreeNode = Me.CharacterTreeView.GetNodeAt(MousePosition)
        Dim c As Character = AllCharacters.Find(n.Text)
        If c.Recovery > 0 Then
            If c.CurrentEndurance + c.Recovery < c.Endurance Then
                c.CurrentEndurance += c.Recovery
            Else
                c.CurrentEndurance = c.Endurance
            End If
            If c.CurrentStun + c.Recovery < c.Stun Then
                c.CurrentStun += c.Recovery
            Else
                c.CurrentStun = c.Stun
            End If
        End If
    End Sub

    Private Sub ApplyDamageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApplyDamageToolStripMenuItem.Click
        Dim n As TreeNode = Me.CharacterTreeView.GetNodeAt(MousePosition)
        Dim c As Character = AllCharacters.Find(n.Text)
        Dim dam As New Damage(c, True)
        dam.ShowDialog()
    End Sub

    Private Sub ApplyHealingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApplyHealingToolStripMenuItem.Click
        Dim n As TreeNode = Me.CharacterTreeView.GetNodeAt(MousePosition)
        Dim c As Character = AllCharacters.Find(n.Text)
        Dim dam As New Damage(c, False)
        dam.ShowDialog()
    End Sub

    Private Sub ActionDoneToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ActionDoneToolStripMenuItem.Click
        Dim n As TreeNode = Me.CharacterTreeView.GetNodeAt(MousePosition)
        n.Checked = True
    End Sub
End Class
